//
//  ViewController.m
//  NiuNiuPacking
//
//  Created by shaoting on 14-9-24.
//  Copyright (c) 2014年 Reseek. All rights reserved.
//

#import "ViewController.h"
#import "Toast+UIView.h"
#import "ParkingInfoViewController.h"

@interface ViewController ()

@end

@implementation ViewController
CLLocationManager * locationManager;
- (IBAction)showMenu {
    
    /*查询本地token
     self.bl = [[TokenBL alloc] init];
     NSMutableArray* listData = [self.bl findAll];
     
     Token* token = [listData firstObjectCommonWithArray:listData];
     if (token != nil) {
     self.localToken.text = token.content;
     [self.bl removeAll];
     }
     
     Token *newToken = [[Token alloc] init];
     newToken.content = self.serverToken.text;
     [self.bl createToken: newToken];
     
     self.localNewToken.text = self.serverToken.text;
     */
    
     [self.frostedViewController  presentMenuViewController];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    locationManager =[[CLLocationManager alloc] init];
    
    [locationManager requestAlwaysAuthorization];//用这个方法，plist中需要NSLocationAlwaysUsageDescription
    
    [locationManager requestWhenInUseAuthorization];//用这个方法，plist里要加字段NSLocationWhenInUseUsageDescription
     [self searchNearParking];
    //显示当前定位
    if ([CLLocationManager locationServicesEnabled])
    {
        _mapView.mapType = MKMapTypeStandard;
        _mapView.delegate = self;
        _mapView.showsUserLocation = YES;
        [_mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
    }
    
   
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


- (void)searchNearParking
{
    //显示当前所在位置附近停车场
   
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:@"陆家嘴" completionHandler:^(NSArray *placemarks, NSError *error) {
        
        NSLog(@"查询结果数量：%i",[placemarks count]);
        
        if ([placemarks count] > 0) {
            [_mapView removeAnnotations:_mapView.annotations];
        }
        
        for (int i = 0; i < [placemarks count]; i++) {
            
            CLPlacemark* placemark = placemarks[i];
            //调整地图位置和缩放比例
            MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, 5000, 5000);
            [_mapView setRegion:viewRegion animated:YES];
            
            MapLocation *annotation = [[MapLocation alloc] init];
            
            annotation.parkingName = @"金茂大厦停车场";
            annotation.parkingAddress = @"陆家嘴环路165号";//placemark.thoroughfare;
            annotation.parkingLot = @"25";
            annotation.coordinate = placemark.location.coordinate;
            
            [_mapView addAnnotation:annotation];
        }
    }];
    
}


#pragma mark -
#pragma mark Map View Delegate Methods
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    _mapView.centerCoordinate = userLocation.location.coordinate;
    //地图缩放
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 5000, 5000);
    [_mapView setRegion:viewRegion animated:YES];
}

- (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error
{
    NSLog(@"error : %@",[error description]);
}

#pragma mark -
#pragma mark Map View Delegate Methods
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
    
    MKPinAnnotationView *annotationView=nil;
    static NSString *indentifer = @"QuanFangPinID";
    
    if(annotation != _mapView.userLocation)     //显示用户位置的那个蓝点
    {
    
    annotationView= (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"];
    if(annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                         reuseIdentifier:@"PIN_ANNOTATION"];
    }
        
        
        for (UIView * view in [annotationView subviews])
            
        {
            
            [view removeFromSuperview];
            
        }
        
    annotationView.image=nil;//[UIImage imageNamed:@"icon_nav_waypoint.png"];
    annotationView.pinColor = MKPinAnnotationColorPurple;
    annotationView.animatesDrop = YES;
    annotationView.canShowCallout = YES;
        
        /*自定义泡泡*/
        
        UIImage *image = [UIImage imageNamed:@"anjuke_icon_itis_position.png"];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        
        imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        
        [annotationView addSubview: imageView];
        
        //[imageView release];
        
        /*泡泡左侧图片
         UIImage *leftImage = [UIImage imageNamed:@"options"];
        
        UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        leftButton.frame = CGRectMake(0, 0, leftImage.size.width, leftImage.size.height);
        [leftButton setBackgroundImage:leftImage forState:UIControlStateNormal];
        leftButton.tag = 1 ;
        annotationView.leftCalloutAccessoryView = leftButton;*/
        
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];;
        rightButton.tag = 2;
        annotationView.rightCalloutAccessoryView = rightButton;
    
    }
    return annotationView;
}

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
    
    if ([control isKindOfClass:[UIButton class]]) {
        if (control.tag == 1) {
            [self showAlertView:@"LeftButton" AndMessage:view.annotation.title];
            NSLog(@"%@",view.annotation.title) ;
        }else if (control.tag == 2){
            
            UINavigationController *navigationController = (UINavigationController *)self.frostedViewController.contentViewController;
            
            ParkingInfoViewController *parkinginfolViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"parkingInfoController"];
            
            
            self.passDelegate = parkinginfolViewController;
            [navigationController pushViewController:parkinginfolViewController animated:YES];
            [self.passDelegate passParameterToView:view.annotation.title];
           // [self showAlertView:@"RightButton" AndMessage:view.annotation.title];
            NSLog(@"%@",view.annotation.subtitle) ;
        }
    }
}
-(void)showAlertView:(NSString *)title AndMessage :(NSString *)message{
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%@ is pressed!",title] message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    [alertView show];
   // [alertView release];
    
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
   //隐藏泡泡事件
    // [self.view makeToast:@"Deselect！"
     //           duration:1.0
        //        position:@"bottom"];
    
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
   // 显示泡泡事件
   // [self.view makeToast:@"didSelect！"
      //          duration:1.0
       //         position:@"bottom"];
}

@end
